using Microsoft.EntityFrameworkCore;
using WinBookKeeping.Models;

namespace WinBookKeeping.Data
{
    public class BookKeepingRepository : IBookKeepingRepository
    {
        private readonly BookKeepingContext _context;

        public BookKeepingRepository()
        {
            _context = new BookKeepingContext();
        }

        // 收入相关操作实现
        public async Task<List<IncomeInfo>> GetAllIncomesAsync()
        {
            return await _context.Incomes.ToListAsync();
        }

        public async Task<IncomeInfo> GetIncomeByIdAsync(int id)
        {
            return await _context.Incomes.FindAsync(id);
        }

        public async Task AddIncomeAsync(IncomeInfo income)
        {
            await _context.Incomes.AddAsync(income);
            await _context.SaveChangesAsync();
        }

        public async Task UpdateIncomeAsync(IncomeInfo income)
        {
            _context.Incomes.Update(income);
            await _context.SaveChangesAsync();
        }

        public async Task DeleteIncomeAsync(int id)
        {
            var income = await _context.Incomes.FindAsync(id);
            if (income != null)
            {
                _context.Incomes.Remove(income);
                await _context.SaveChangesAsync();
            }
        }

        public async Task<List<IncomeInfo>> GetIncomesByDateRangeAsync(DateTime startDate, DateTime endDate)
        {
            return await _context.Incomes
                .Where(i => DateTime.Parse(i.IncomeTime) >= startDate && DateTime.Parse(i.IncomeTime) <= endDate)
                .ToListAsync();
        }

        // 支出相关操作实现
        public async Task<List<ExpendInfo>> GetAllExpendituresAsync()
        {
            return await _context.Expenditures.ToListAsync();
        }

        public async Task<ExpendInfo> GetExpenditureByIdAsync(int id)
        {
            return await _context.Expenditures.FindAsync(id);
        }

        public async Task AddExpenditureAsync(ExpendInfo expenditure)
        {
            await _context.Expenditures.AddAsync(expenditure);
            await _context.SaveChangesAsync();
        }

        public async Task UpdateExpenditureAsync(ExpendInfo expenditure)
        {
            _context.Expenditures.Update(expenditure);
            await _context.SaveChangesAsync();
        }

        public async Task DeleteExpenditureAsync(int id)
        {
            var expenditure = await _context.Expenditures.FindAsync(id);
            if (expenditure != null)
            {
                _context.Expenditures.Remove(expenditure);
                await _context.SaveChangesAsync();
            }
        }

        public async Task<List<ExpendInfo>> GetExpendituresByDateRangeAsync(DateTime startDate, DateTime endDate)
        {
            return await _context.Expenditures
                .Where(e => DateTime.Parse(e.expendTime) >= startDate && DateTime.Parse(e.expendTime) <= endDate)
                .ToListAsync();
        }

        // 名目相关方法实现
        public async Task<List<RecordInfo>> GetAllItemsAsync()
        {
            using var context = new BookKeepingContext();
            return await context.Items.ToListAsync();
        }

        public async Task<RecordInfo> GetItemByIdAsync(int id)
        {
            using var context = new BookKeepingContext();
            return await context.Items.FindAsync(id);
        }

        public async Task AddItemAsync(RecordInfo item)
        {
            using var context = new BookKeepingContext();
            await context.Items.AddAsync(item);
            await context.SaveChangesAsync();
        }

        public async Task UpdateItemAsync(RecordInfo item)
        {
            using var context = new BookKeepingContext();
            context.Items.Update(item);
            await context.SaveChangesAsync();
        }

        public async Task DeleteItemAsync(int id)
        {
            using var context = new BookKeepingContext();
            var item = await context.Items.FindAsync(id);
            if (item != null)
            {
                context.Items.Remove(item);
                await context.SaveChangesAsync();
            }
        }
    }
} 